define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        console.log(window.location.search)
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    var ids = getQueryString('ids')
    var user_id = getQueryString('user_id')
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/vipapply/index',
                    add_url: 'pro/vipapply/add',
                    edit_url: 'pro/vipapply/edit',
                    del_url: 'pro/vipapply/del',
                    multi_url: 'pro/vipapply/multi',
                    table: 'gzns_user_vip_apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    if (!params.filter) {
                        params.filter={}
                    }else {
                        params.filter = JSON.parse(params.filter)
                    }
                    if (!params.op) {
                        params.op={}
                    }else {
                        params.op = JSON.parse(params.op)

                    }
                    if (ids) {
                        params.filter.user_id = user_id;
                        params.op.user_id = '='
                    }

                    params.filter = JSON.stringify(params.filter)
                    params.op = JSON.stringify(params.op)

                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        // {field: 'vip_type', title: __('Vip_type'), searchList: {"1":__('Vip_type 1'),"2":__('Vip_type 2')}, formatter: Table.api.formatter.status},
                        // {field: 'level.name', title: __('Level.name'), operate: 'Like'},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'real_name', title: __('Real_name')},
                        // {field: 'identity_code', title: __('Identity_code')},
                        {field: 'real_name', title: __('Real_name')},
                      {field: 'en_name', title: __('En_name')},
                      {field: 'phone', title: __('Phone')},
                      {field: 'sex', title: __('Sex'), searchList: {"0":__('Sex 0'),"1":__('Sex 1'),"2":__('Sex 2')}, formatter: Table.api.formatter.status},
                        {field: 'native_place', title: __('Native_place')},
                        // {field: 'address', title: __('Address')},
                        // {field: 'company_name', title: __('Company_name'),operate: 'Like'},
                        // {field: 'company_credit_code', title: __('Company_credit_code'),operate: 'Like'},
                        // {field: 'company_principal', title: __('Company_principal'),operate: 'Like'},
                        // {field: 'info.nick_name', title: __('Info.nick_name'),operate: 'Like'},
                        // {field: 'info.avatar', title: __('Info.avatar'), formatter: Table.api.formatter.image,operate: false},
                        // {field: 'info.phone', title: __('Info.phone'),operate: 'Like'},
                        // {field: 'info.real_name', title: __('Info.real_name'),operate: 'Like'},
                        // {field: 'info.identity_code', title: __('Info.identity_code'),operate: 'Like'},
                        // {field: 'info.post', title: __('职务'),operate: 'Like'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $('#c-vip_level_id').data("params", function (obj) {
                return {custom: {vip_type: $('#c-vip_type').val()}};
            });

            $('#c-vip_type').on('change',function () {
                $('#c-vip_level_id').selectPageClear();
            })
            $('[data-submit]').click(function () {
                Layer.confirm('修改信息后，用户已提交的待审核转账订单自动关闭，请知会用户重新上传支付凭证，线上支付不受影响，确认操作吗？', {icon: 3, title:'提示'}, function(index){
                    //do something
                    $("form[role=form]").submit()
                    Layer.close(index);
                });
            })
            $('[data-status]').click(function () {
                var status = $(this).attr('data-status')
                $('#c-status').val(status)
                $('#c-status-select').val(status)
                var tip = '确认吗'
                if(status == 0){
                    tip = '确认要设为不通过吗？'
                } else if(status == 2){
                    tip = '审核通过后用户可支付费用成为会员，确认会员信息、会员时长和价格设置无误？'
                } else if(status == 3){
                  tip = '确认要设为通过审核？'
                } else if(status == 4){
                    tip = '设为取消后，用户可重新提交申请，确认取消吗？'

                }
                Layer.confirm(tip, {icon: 3, title:'提示'}, function(index){
                    //do something
                    $("form[role=form]").submit()
                    Layer.close(index);
                });
                //
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
